package com.buaa.aiguangxi.service;

import com.buaa.aiguangxi.dao.UserDao;
import com.buaa.aiguangxi.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserService implements UserDetailsService {
    @Autowired
    private UserDao userDao;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user=userDao.getUserByName(username);
        List<SimpleGrantedAuthority> list=new ArrayList<>();
        if (user!=null){
            list.add(new SimpleGrantedAuthority("user"));
        }
        else {
            throw new UsernameNotFoundException("用户不存在！");
        }
        return new org.springframework.security.core.userdetails.User(user.getUsername(),user.getUserPassword(),list);
    }
    public User getUserByName(String username){
        return userDao.getUserByName(username);
    }
    public int addUser(User user) {
        return userDao.addUser(user);
    }

}
